package com.mgr.controller;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mgr.service.OTPService;
import com.mgr.service.SmsService;

@Controller
public class InternalTransferController4 {
	@Autowired
	private OTPService otpService;

	@Autowired
	private SmsService smsService;

	private ResourceBundle rb = ResourceBundle
			.getBundle("com.mgr.messages.errorMessage");

	@RequestMapping("/internaltransfer3")
	public ModelAndView setFormTransferStep2(HttpSession session,
			HttpServletRequest request
	) {
		String answer = request.getParameter("answer");
		ModelAndView modelAndView = new ModelAndView(
				"internaltransaction4.tile");
		String messageCapt = "";
		Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
		if (session.getAttribute("user") == null) {
			session.invalidate();
			modelAndView.setViewName("login");
		} else if (answer != null && !captcha.isCorrect(answer)) {
			messageCapt = rb.getString("5");
			modelAndView.setViewName("internaltransaction3.tile");
			modelAndView.addObject("messageCapt", messageCapt);
		} else {
			int custId = Integer.parseInt(session.getAttribute("user")
					.toString());
			String otpCode = smsService.createOTP();// create OTP code
			session.setAttribute("otpCode", otpCode);
			otpService.sendEmail(custId, otpCode);// sent OTP code for user via
													// email
		}
		return modelAndView;
	}
}